package com.tianqu.tms.www.financia.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.TmsFinanciaDto;
import com.tianqu.tms.entity.TmsFinancial;
import com.tianqu.tms.www.financia.actionform.FinanciaCompanyInputActionForm;
import com.tianqu.tms.www.financia.controller.FinanciaCompanyListController;
import com.tianqu.tms.www.financia.viewhelper.FinanciaCompanyInputViewHelper;

public class FinanciaCompanyListAction extends TmsAction {
	private Logger logger = Logger.getLogger(FinanciaCompanyListAction.class);
	public final static String PAGENO = "pageNo";
	public final static String SORTKEY = "sortKey";
	public final static String MAXINDEX = "maxIndex";
	public final static String ASC = "asc";
	
	
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response, UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		FinanciaCompanyInputActionForm searchForm = (FinanciaCompanyInputActionForm)form;
		FinanciaCompanyInputViewHelper view = new FinanciaCompanyInputViewHelper();
		BeanUtils.copyProperties(view, searchForm);
 
		if(request.getParameter(PAGENO) == null) {
			uc.setAttribute(PAGENO, view.getPageNo());
			uc.setAttribute(SORTKEY, view.getSortKey());
			uc.setAttribute(MAXINDEX, view.getMaxIndex());
			uc.setAttribute(ASC, view.getAsc());
		} else {
			uc.setAttribute(PAGENO, request.getParameter(PAGENO));
			uc.setAttribute(SORTKEY, request.getParameter(SORTKEY));
			uc.setAttribute(MAXINDEX, request.getParameter(MAXINDEX));
			uc.setAttribute(ASC, request.getParameter(ASC));
		}
		uc.setAttribute(TmsConstants.VIEWHELP, view);
		FinanciaCompanyListController controller = new FinanciaCompanyListController();
		controller.execute(uc);
		
		TmsFinancial financial = (TmsFinancial)uc.getAttribute(FinanciaCompanyListController.PAGING);
		List<TmsFinanciaDto> list = (List<TmsFinanciaDto>)uc.getAttribute(FinanciaCompanyListController.DATA);
		int i = 0;
		for (TmsFinanciaDto tmsFinanciaDto : list) {
			if( i % 2 == 0) tmsFinanciaDto.setCssFlag(1);
			else tmsFinanciaDto.setCssFlag(2);
		}
		view.setPageNo(Integer.toString(financial.getPageNo()));
		view.setMaxIndex(Integer.toString(financial.getMaxIndex()));
		view.setAsc(Boolean.toString(financial.isAsc()));
		view.setSortKey(Integer.toString(financial.getSortKey()));
		view.setTotalCount(Integer.toString(financial.getTotalCount()));
		view.setDateFrom(Integer.toString(financial.getDateFrom()));
		view.setDateTo(Integer.toString(financial.getDateTo()));
		view.setViewList(list);
		
		request.setAttribute(TmsConstants.VIEWHELP, view);		
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
